/****************************************************************************
** Uses Econographics corrlation data to produce correlations				*
** Modifies existing code to try robustness to other PCA-like techniques 	* 	
** with full dataset: 														*			
** ORIV with 8 components (Kaiser Criterion)								*
** ORIV unweighted (this and all other have 6 components)					*
** Averages (weighted)														*
** Spearman (unweighted, by necessity)										*
** Factor analysis															*
**																			*
** Produced by Erik Snowberg, October 13, 2022								*
** 		Based on Code by Jonathan Chapman and Erik Snowberg					*
****************************************************************************/

/**********************************************************************
** Create the correlation matrix
**********************************************************************/
/**********************************
**Define macros needed to run code
***********************************/
// Number of bootstrap replications for SEs (0=asymptotic SEs)
// Note 10000 repetitions takes around 10minutes for 1 correlation
global bootstrapReps=0

// List of variables with two measures. Note that it is assumed that these measures
// are labeled "1" and "2". Hence the renaming of some variables
global  twoByTwoList =`"WTP WTA riskAversionCRSure riskAversionCRLottery commonRatio endowmentEffect "' ///		
					+ `"advanInequalityAversion disadInequalityAversion patience riskAversionUrn riskAversionGain "' ///
					+ `"riskAversionMixGainLoss riskAversionLoss "' ///		
					+ `"ambiguityAversion compoundAversion qualOverPrecise "' ///
					+ `"overPlacementFacts overEstimateIQ reciprocityHigh reciprocityLow "'
// List of variables with measures where non-orthogonal error is an issue 
// since the measures are constructed with common compoenents
** These need to entered in PAIRS relating to the correlation that should be implemented
** with the separate procedure
** e.g. if want the correlation with endowment effect and WTP and also WTA, endowment effect enters twice
** NOTE: all these variables should also be included in the twoByTwoList above
global  twoByTwoListNonOrthog =`"ambiguityAversion compoundAversion "' ///
							+ `"endowmentEffect WTA "' ///
							+ `"endowmentEffect WTP "' ///
							+ `"commonRatio riskAversionCRSure "' ///
							+ `"commonRatio riskAversionCRLottery "' ///
							+ `"ambiguityAversion riskAversionUrn "' ///
							+ `"compoundAversion riskAversionUrn "' ///
							+ `"qualOverPrecise overPlacementFacts "' 

// List of variables with one measure 
global  oneByTwoListEV =`"altruism trust punishSender punishReceiver PSext PRext"'
					
	
/*****************
**Run the Code
******************/
// This defines a program to run the non-orthogonal ORIV procedure, 
// and then constructs separate matrices with the correlations and SEs
// note that these matricies are all stored in Stata memory
use "${datapath}econographicsForPCA.dta", clear
do "${dofilepath}createCorrelationMatrixForPCA.do"

//variables are ordered social + risk + other
#delimit;
local varlistAll "reciprocityLow reciprocityHigh altruism trust punishSender 
					punishReceiver advanInequalityAversion disadInequalityAversion 
					WTA riskAversionGain riskAversionLoss riskAversionMixGainLoss 
					WTP riskAversionCRSure riskAversionCRLottery 
					ambiguityAversion compoundAversion
					overEstimateIQ overPlacementFacts qualOverPrecise patience";
#delimit cr
// Need to create a subset of the correlation matrix with these variables
** First the columns
cap matrix drop B
local r=rowsof(corr2)
foreach i in `varlistAll'  {
  matrix B= nullmat(B), corr2[1..`r',colnumb(corr2,"`i'")]
}
** Now the rows
cap matrix drop C
local c=colsof(B)
foreach i in  `varlistAll'  {
  matrix C= nullmat(C) \ B[rownumb(B,"`i'"),1..`c']  
}

cap mat drop MAll
mat rename C MAll

// PCA (make sure number of obs is correct)
pcamat MAll, n(1000) forcepsd components(5)
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPFiveComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity", "RA WTA", "WTP / Inequality", "Uncertainty/Overconfidence" , "Impulsivity/Uncertainty", "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	

// PCA (make sure number of obs is correct)
pcamat MAll, n(1000) forcepsd components(7)
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPSevenComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity", "RA:WTA", "WTP/Inequality" , "Punishment" , "Uncertainty", "Overconfidence 1" , "Overconfidence 2", "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	


// PCA (make sure number of obs is correct)
pcamat MAll, n(1000) forcepsd components(8)
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPEightComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "RA: WTA", "WTP / Inequality", "Reciprocity" , "Altruism / Trust" , "Punishment" , "Uncertainty", "Overconfidence 1" , "Overconfidence 2", "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	

/**********************************************************************
** ORIV Correlation Matrix (unweighted)				  *
**********************************************************************/
// Same as above, except correlation matrix is unweighted
global bootstrapReps=0

global  twoByTwoList =`"WTP WTA riskAversionCRSure riskAversionCRLottery commonRatio endowmentEffect "' ///		
					+ `"advanInequalityAversion disadInequalityAversion patience riskAversionUrn riskAversionGain "' ///
					+ `"riskAversionMixGainLoss riskAversionLoss "' ///		
					+ `"ambiguityAversion compoundAversion qualOverPrecise "' ///
					+ `"overPlacementFacts overEstimateIQ reciprocityHigh reciprocityLow "'

global  twoByTwoListNonOrthog =`"ambiguityAversion compoundAversion "' ///
							+ `"endowmentEffect WTA "' ///
							+ `"endowmentEffect WTP "' ///
							+ `"commonRatio riskAversionCRSure "' ///
							+ `"commonRatio riskAversionCRLottery "' ///
							+ `"ambiguityAversion riskAversionUrn "' ///
							+ `"compoundAversion riskAversionUrn "' ///
							+ `"qualOverPrecise overPlacementFacts "' 

global  oneByTwoListEV =`"altruism trust punishSender punishReceiver PSext PRext"'
					
	
use "${datapath}econographicsForPCA.dta", clear
do "${dofilepath}createCorrelationMatrixForPCAunweighted.do"

//variables are ordered social + risk + other
#delimit;
local varlistAll "reciprocityLow reciprocityHigh altruism trust punishSender 
					punishReceiver advanInequalityAversion disadInequalityAversion 
					WTA riskAversionGain riskAversionLoss riskAversionMixGainLoss 
					WTP riskAversionCRSure riskAversionCRLottery 
					ambiguityAversion compoundAversion
					overEstimateIQ overPlacementFacts qualOverPrecise patience";
#delimit cr
// Need to create a subset of the correlation matrix with these variables
** First the columns
cap matrix drop B
local r=rowsof(corr2)
foreach i in `varlistAll'  {
  matrix B= nullmat(B), corr2[1..`r',colnumb(corr2,"`i'")]
}
** Now the rows
cap matrix drop C
local c=colsof(B)
foreach i in  `varlistAll'  {
  matrix C= nullmat(C) \ B[rownumb(B,"`i'"),1..`c']  
}

cap mat drop MAll
mat rename C MAll

// PCA (make sure number of obs is correct)
pcamat MAll, n(1000) forcepsd components(6)
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPUnweightedComponents.tex, 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity" , "RA: WTA" , "WTP / Inequality", "Impulsivity" , "Overconfidence" , "Uncertainty", "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	

/**********************************************************************
** Dataset for Averages	(needed for averages, spearman, FA)		  	  *
**********************************************************************/
use "${datapath}econographicsForPCA.dta", clear

global  twoByTwoList =`"WTP WTA riskAversionCRSure riskAversionCRLottery commonRatio endowmentEffect "' ///		
					+ `"advanInequalityAversion disadInequalityAversion patience riskAversionUrn riskAversionGain "' ///
					+ `"riskAversionMixGainLoss riskAversionLoss "' ///		
					+ `"ambiguityAversion compoundAversion qualOverPrecise "' ///
					+ `"overPlacementFacts overEstimateIQ reciprocityHigh reciprocityLow "'

global  twoByTwoListNonOrthog =`"ambiguityAversion compoundAversion "' ///
							+ `"endowmentEffect WTA "' ///
							+ `"endowmentEffect WTP "' ///
							+ `"commonRatio riskAversionCRSure "' ///
							+ `"commonRatio riskAversionCRLottery "' ///
							+ `"ambiguityAversion riskAversionUrn "' ///
							+ `"compoundAversion riskAversionUrn "' ///
							+ `"qualOverPrecise overPlacementFacts "' 

foreach var in $twoByTwoList $twoByTwoListNonOrthog {
	cap drop avg_`var'
	gen avg_`var'=(`var'1+`var'2)/2
}

/**********************************************************************
** Averages		  *
**********************************************************************/

#delimit ; 
	pca avg_reciprocityLow avg_reciprocityHigh altruism trust punishSender punishReceiver avg_advanInequalityAversion avg_disadInequalityAversion 
	avg_WTA avg_riskAversionGain avg_riskAversionLoss avg_riskAversionMixGainLoss avg_WTP avg_riskAversionCRSure avg_riskAversionCRLottery 
	avg_ambiguityAversion avg_compoundAversion 
	avg_overEstimateIQ avg_overPlacementFacts avg_qualOverPrecise 
	avg_patience
	[aweight=weight],comp(6)
;
#delimit cr 
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPAverageComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity" , "RA: WTA" , "WTP / Inequality", "Impulsivity", "Uncertainty", "Overconfidence" , "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	

*********************************************************************************************************
// Let's try pca with spearman correlations (terrible idea!)
*********************************************************************************************************
** Save Spearman correlation matrix
#delimit ; 
	spearman avg_reciprocityLow avg_reciprocityHigh altruism trust punishSender punishReceiver avg_advanInequalityAversion avg_disadInequalityAversion 
	avg_WTA avg_riskAversionGain avg_riskAversionLoss avg_riskAversionMixGainLoss avg_WTP avg_riskAversionCRSure avg_riskAversionCRLottery 
	avg_ambiguityAversion avg_compoundAversion 
	avg_overEstimateIQ avg_overPlacementFacts avg_qualOverPrecise 
	avg_patience
;
#delimit cr 
mat spearmanAll=r(Rho)

pcamat spearmanAll, n(1000)  forcepsd comp(6)
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPSpearmanComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity" , "RA: WTA" , "WTP / Inequality", "Impulsivity", "Uncertainty", "Overconfidence" , "Unexplained")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
#delimit cr	

/**********************************************************************
** Factor Analysis	  *
**********************************************************************/

** FA with 6 components
#delimit ; 
	factor avg_reciprocityLow avg_reciprocityHigh altruism trust punishSender punishReceiver avg_advanInequalityAversion avg_disadInequalityAversion 
	avg_WTA avg_riskAversionGain avg_riskAversionLoss avg_riskAversionMixGainLoss avg_WTP avg_riskAversionCRSure avg_riskAversionCRLottery 
	avg_ambiguityAversion avg_compoundAversion 
	avg_overEstimateIQ avg_overPlacementFacts avg_qualOverPrecise 
	avg_patience [aweight=weight],comp(6)
;
#delimit cr 
rotate

// Output compenents to Latex Table
cap mat drop out
matrix out = e(r_L) , e(Psi)'
#delimit;
frmttable using ${outputpath}APPFactorComponents.tex , 
		tex statmat(out) sdec(2) replace fragment plain nocenter
		ctitles("", "Generosity" , "RA: WTA" , "WTP / Inequality", "Uncertainty" , "Impulsivity" , "Overconfidence", "Uniqueness")
		rtitles("Reciprocity: Low" \ 
				"Reciprocity: High" \
				"Altruism" \ 
				"Trust" \
				"Anti-Social Punishment" \ 
				"Pro-Social Punishment" \
				"Dislike Having More" \ 
				"Dislike Having Less" \
				"WTA" \ 
				"Risk Aversion: Gains" \ 
				"Risk Aversion: Losses" \ 
				"Risk Aversion: Gain / Loss" \
				"WTP" \ 
				"Risk Aversion: CR Certain" \ 
				"Risk Aversion: CR Lottery" \ 
				"Ambiguity Aversion" \ 
				"Compound Lottery Aversion"\
				"Overestimation" \ 
				"Overplacement" \ 
				"Overprecision" \ 
				"Patience");
